package main.od;

import java.util.ArrayList;
import java.util.Scanner;

public class BestServerLocation {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int nums = Integer.parseInt(sc.nextLine());
        int[][] locationArr = new int[nums][2];
        for (int i = 0;i < nums;i++){
            locationArr[i][0] = sc.nextInt();
            locationArr[i][1] = sc.nextInt();
        }
        int minDistances = getBestLocation(locationArr);
        System.out.println("最小值为： " + minDistances);
    }

    public static int getBestLocation(int[][] locationArr){
        // the best location of server station
        int beatLocation;
        // the summary distance
        int minDistances = Integer.MAX_VALUE;
        ArrayList<Integer> temp = new ArrayList<>();
        for (int[] location : locationArr){
            temp.add(location[0]);
            temp.add(location[1]);
        }
        temp.sort(Integer::compareTo);
        int min = temp.get(0);
        int max = temp.get(temp.size() - 1);
        for (int i = min;i <= max;i++){
            int distances = 0;
            for (int[] location : locationArr){
                if (location[0] > i){
                    distances += location[0] - i;
                }else if (location[1] < i){
                    distances += i - location[1];
                }
            }
            minDistances = Math.min(distances,minDistances);
        }
        return minDistances;
    }

}
